package com.bah.tract.mapping;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;

import com.bah.tract.mapping.PropertyHelper;
import com.bah.tract.service.Property;

public final class WorkStoreFactory {
	private WorkStoreFactory() {

	}

	public static com.bah.tract.service.WorkStore getWorkStore(
			com.bah.tract.model.WorkStore modelStore) {
		com.bah.tract.service.WorkStore store = new com.bah.tract.service.WorkStore();
		try {
			BeanUtils.copyProperties(store, modelStore);
			Set<?> basicFields = BeanUtils.describe(store).keySet();
			Map<?, ?> allFields = BeanUtils.describe(modelStore);
			// toss out the basicFields. We've already copied those.
			for (Map.Entry<?, ?> entry : allFields.entrySet()) {
				if (!basicFields.contains(entry.getKey())) {
					Property prop = PropertyHelper.getProperty(entry.getKey()
							.toString(), entry.getValue());
					store.getProperty().add(prop);
				}
			}
			return store;
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(e);
		} catch (InvocationTargetException e) {
			throw new IllegalArgumentException(e);
		} catch (NoSuchMethodException e) {
			throw new IllegalArgumentException(e);
		}
	}
}
